<template>
  <div class="student-main">
    <el-card class="box-card" style="margin: 20px;">
      <div slot="header" class="clearfix">
        <el-button type="primary" plain style="float: left; padding: 10px" @click="toAdd">新增</el-button>
      </div>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input v-model="search.name" placeholder="请输入姓名"></el-input>
        </el-col>
        <el-col :span="6">
          <el-select v-model="search.classId" placeholder="请选择班级" clearable>
            <el-option
              v-for="item in classlist"
              :key="item.id"
              :label="item.className"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-col>
        <el-col :span="6">
          <el-date-picker
            type="date"
            placeholder="开课时间"
            v-model="search.startTime"
            value-format="yyyy-MM-dd"
            style="width: 100%;"
          ></el-date-picker>
        </el-col>
        <el-col :span="6">
          <el-button type="info">重置</el-button>
          <el-button type="primary" @click="doSearch">查询</el-button>
        </el-col>
      </el-row>
    </el-card>
    <el-table
      v-loading="loading"
      :data="studentlist"
      border
      style="width: calc(100% - 40px); margin: 20px"
    >
      <el-table-column fixed type="index" label="序号" width="100"></el-table-column>
      <!-- <el-table-column prop="addTime" label="日期" width="150"></el-table-column> -->
      <el-table-column prop="name" label="姓名" width="120"></el-table-column>
      <el-table-column label="性别" width="120">
        <template slot-scope="scope">
          <span>{{scope.row.sex == '0'? '男': '女'}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="cityText" label="地址" width="200"></el-table-column>
      <el-table-column prop="tel" label="电话" width="120"></el-table-column>
      <el-table-column prop="addTime" label="新增日期" width="200"></el-table-column>
      <el-table-column prop="zip" label="邮编" width="120"></el-table-column>
      <el-table-column fixed="right" label="操作" width="160">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            circle
            @click="edit(scope.row.id)"
          >修改</el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            circle
            @click="del(scope.row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <newS ref="newS" @update="getlist" :classlist="classlist"></newS>
    <!-- 分页 -->
    <div class="block" style="float:right; margin:20px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="start"
        :page-sizes="sizes"
        :page-size="limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalcount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { getstudentList, del } from "@/api/xy/student";
import { classlist } from "@/api/xy/clazz";
import axios from "axios";
const baseUrl = "http://www.bufantec.com";
import newS from "./newS";
export default {
  components: { newS },
  data() {
    return {
      search: {
        name: ""
      },
      start: 1,
      limit: 10,
      sizes: [10, 20, 30, 50],
      totalcount: 0,
      loading: false,
      studentlist: [],
      classlist: [],
      studentDetail: {}
    };
  },
  created() {
    this.getlist();
    this.getclasslist();
  },
  methods: {
    doSearch() {
      this.start = 1;
      this.getlist();
    },
    getlist() {
      this.loading = true;
      getstudentList({
        start: this.start,
        limit: this.limit,
        ...this.search,
        status: 0
      }).then(res => {
        // console.log(res);
        this.studentlist = res.data.list;
        this.totalcount = res.data.totalRow;
        // console.log(res);
        this.loading = false;
      });
    },

    getclasslist() {
      classlist({
        start: 1,
        limit: 10
      }).then(res => {
        this.classlist = res.data.list;
        console.log(this.classlist);
      });
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.limit = val;
      this.getlist();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.start = val;
      this.getlist();
    },

    toAdd() {
      // console.log(this.$refs.newS);
      this.$refs.newS.open();
    },
    del(id) {
      this.$confirm("您确定要删除当前项吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          del({
            id
          }).then(res => {
            // console.log(res);
            var i = this.studentlist.findIndex(el => el.id == id);
            this.studentlist.splice(i, 1);
            if (res.code == true) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
            }
            this.getlist();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消删除"
          });
        });
    },
    edit(id) {
      // console.log(row);
      this.$refs.newS.open(id);
    }
  }
};
</script>

<style lang="scss" scoped>
</style>